package com.noah.project.controller

import com.noah.project.dao.CompanyDao
import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.global.TokenManager
import com.noah.project.model.Company
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class CompanyApiController {
    @Autowired
    lateinit var companyDao: CompanyDao
    val log = LoggerFactory.getLogger(CompanyApiController::class.simpleName)

    /*****************公司******************/
    @RequestMapping(Const.API_ADD_COMPANY)
    fun addCompany(company: Company, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (companyDao.findCompany(company.coid) != null) {
                return Reply(code = Const.ERROR_ALREADY, msg = "已经创建过的公司")
            }
            if (companyDao.insert(company) > 0) {
                return Reply(code = Const.SUCCESS, msg = "创建公司成功", data = JsonManager.toJson(company))
            }
            return Reply(code = Const.FAIL, msg = "创建公司失败")
        } catch (e: Exception) {
            log.error("创建公司", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "公司异常")
        }
    }

    @RequestMapping(Const.API_DELETE_COMPANY)
    fun deleteCompany(company: Company, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (company.coid != 0L) {
                if (companyDao.findCompany(company.coid) == null) {
                    return Reply(code = Const.ERROR_ALREADY, msg = "没有公司可以删除")
                }
                if (companyDao.delete(company.coid) > 0) {
                    return Reply(code = Const.SUCCESS, msg = "删除公司成功")
                }

            } else if (company.aid != 0L) {
                if (companyDao.findCompanies(company.aid).isEmpty()) {
                    return Reply(code = Const.ERROR_ALREADY, msg = "没有公司可以删除")
                }
                if (companyDao.deleteAll(company.aid) > 0) {
                    return Reply(code = Const.SUCCESS, msg = "删除公司成功")
                }
            }
            return Reply(code = Const.FAIL, msg = "删除公司失败")
        } catch (e: Exception) {
            log.error("删除公司", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "公司异常")
        }
    }

    @RequestMapping(Const.API_UPDATE_COMPANY)
    fun updateCompany(company: Company, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = companyDao.update(company)
            if (ret > 0) {
                val retCompany = companyDao.findCompany(company.coid)
                return Reply(code = Const.SUCCESS, msg = "修改成功", data = JsonManager.toJson(retCompany))
            }
            return Reply(code = Const.FAIL, msg = "修改失败")
        } catch (e: Exception) {
            log.error("更新公司", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "公司异常")
        }
    }

    @RequestMapping(Const.API_GET_COMPANY_LIST)
    fun getCompanyList(aid: Long): Reply {
        try {
            val companies = companyDao.findCompanies(aid)
            if (companies.isNotEmpty()) {
                return Reply(code = Const.SUCCESS, msg = "获取公司列表成功", data = JsonManager.toJson(companies))
            }
            return Reply(code = Const.FAIL, msg = "没有更多公司")
        } catch (e: Exception) {
            log.error("公司列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "公司异常")
        }
    }

    @RequestMapping(Const.API_GET_COMPANY)
    fun getCompany(coid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val company =  companyDao.findCompany(coid)
            if (company != null) {
                return Reply(code = Const.SUCCESS, msg = "获取公司成功", data = JsonManager.toJson(company))
            } else {
                return Reply(code = Const.FAIL, msg = "获取公司失败")
            }
        } catch (e: Exception) {
            log.error("获取公司", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "公司异常")
        }
    }
}